package com.shw.loco.adapter;

import java.io.IOException;
import java.lang.reflect.Method;
import java.security.InvalidParameterException;

import android.content.Context;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

import com.shw.loco.util.InfoUtils;

/**
 * 
 * @author HAHA
 * 
 */
public class LocoMainTrigger {
	private static final String TAG = "LocoMainTrigger";
	private SharedPreferences sharedPreferences;
	private String rfid_port;
	private String gpbd_port;
	private String loco_port;
	private String rfid_baudrate;
	private String gpsbd_baudrate;
	private String loco_baudrate;
	private int rfid_time;
	private int gps_time;

	public LocoMainTrigger(SharedPreferences sharedPreferences) {
		this.sharedPreferences = sharedPreferences;
		try {
			rfid_port = sharedPreferences.getString(InfoUtils.RFID_PORT, "");
			if (rfid_port == null || rfid_port.length() < 1)
				throw new Exception("rfid_port exception");
		} catch (Exception e) {
			rfid_port = InfoUtils.SERVER_RFID_PORT;
		}
		try {
			InfoUtils.TERMINALID = sharedPreferences.getString(InfoUtils.LOCOTERMINAL, "0000");
			if (!InfoUtils.TERMINALID.equals("0") || InfoUtils.TERMINALID.length() < 1)
				throw new Exception("InfoUtils.TERMINALID exception");
		} catch (Exception e) {
			InfoUtils.TERMINALID = "0000";
		}
		try {
			gpbd_port = sharedPreferences.getString(InfoUtils.GPSBD_PORT, "");
			if (gpbd_port == null || gpbd_port.length() < 1)
				throw new Exception("gpbd_port exception");
		} catch (Exception e) {
			gpbd_port = InfoUtils.SERVER_GPS_PORT;
		}
		try {
			loco_port = sharedPreferences.getString(InfoUtils.LOCO_PORT, "");
			if (loco_port == null || loco_port.length() < 1)
				throw new Exception("loco_port exception");
		} catch (Exception e) {
			loco_port = InfoUtils.SERVER_LOCO_PORT;
		}
		try {
			rfid_time = Integer.valueOf(sharedPreferences.getString(
					InfoUtils.RFID_TIME, "200"));
			if (rfid_time <= 50)
				throw new Exception("rfid_time exception");
		} catch (Exception e) {
			rfid_time = InfoUtils.SERVER_DELAY;
		}
		try {
			gps_time = Integer.valueOf(sharedPreferences.getString(
					InfoUtils.GPSBD_TIME, "200"));
			if (gps_time <= 50)
				throw new Exception("gps_time exception");
		} catch (Exception e) {
			gps_time = InfoUtils.GPS_DELAY;
		}
		try {
			rfid_baudrate = sharedPreferences.getString(
					InfoUtils.RFID_BAUDRATE, "");
			if (rfid_baudrate == null || rfid_baudrate.length() < 1)
				throw new Exception("rfid_baudrate exception");
		} catch (Exception e) {
			rfid_baudrate = InfoUtils.SERVER_RFID_PAUDRATE;
		}
		try {
			gpsbd_baudrate = sharedPreferences.getString(
					InfoUtils.GPSBD_BAUDRATE, "");
			if (gpsbd_baudrate == null || gpsbd_baudrate.length() < 1)
				throw new Exception("gpsbd_baudrate exception");
		} catch (Exception e) {
			gpsbd_baudrate = InfoUtils.SERVER_GPS_PAUDRATE;
		}
		try {
			loco_baudrate = sharedPreferences.getString(
					InfoUtils.LOCO_BAUDRATE, "");
			if (loco_baudrate == null || loco_baudrate.length() < 1)
				throw new Exception("loco_baudrate exception");
		} catch (Exception e) {
			loco_baudrate = InfoUtils.SERVER_LOCO_PAUDRATE;
		}
	}

	public void startRS485RFID() {
		Thread th = new RSTrigger();
		th.start();
	}

	public static void openConnect(Context context) {
		ConnectivityManager cm = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkInfo = cm.getActiveNetworkInfo();
		Method setMobileDataEnabl = null;
		try {
			if (networkInfo == null || !networkInfo.isAvailable()) {
				setMobileDataEnabl = cm.getClass().getDeclaredMethod(
						"setMobileDataEnabled", boolean.class);
				setMobileDataEnabl.invoke(cm, true);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	class RSTrigger extends Thread {

		private SerialControl control485_RFID;
		private SerialControl control485_LOCO;
		private SerialControl control232_GPBD;

		public RSTrigger() {
			control485_RFID = new SerialControl();
			control485_LOCO = new SerialControl();
			control232_GPBD = new SerialControl();
		}

		@Override
		public void run() {
			// TODO Auto-generated method stub
			super.run();
			try {
				control232_GPBD.setPort(gpbd_port);
				control232_GPBD.setBaudRate(gpsbd_baudrate);
				control232_GPBD.setPortinfo(InfoUtils.PORT232_GPS);
				control232_GPBD.setDelay(gps_time);
				control232_GPBD.open();

				control485_RFID.setPort(rfid_port);
				control485_RFID.setBaudRate(rfid_baudrate);
				control485_RFID.setPortinfo(InfoUtils.PORT485_RFID);
				control485_RFID.setDelay(rfid_time);
				control485_RFID.open();
				try {
					control485_RFID.setHexLoopData(InfoUtils.dateToHexStr());
				} catch (Exception e) {
				}

				control485_LOCO.setPort(loco_port);
				control485_LOCO.setBaudRate(loco_baudrate);
				control485_LOCO.setPortinfo(InfoUtils.PORT485_LOCO);
				control485_LOCO.open();
			} catch (SecurityException e) {
				Log.d(TAG, "打开串口失败:没有串口读/写权限!");
			} catch (IOException e) {
				Log.d(TAG, "打开串口失败:未知错误!");
			} catch (InvalidParameterException e) {
				Log.d(TAG, "打开串口失败:参数错误!");
			}
		}

	}
}
